<script>
	var reStartTimestamp=0;
	var reStartTimer=null;
	import uniPushListener  from  "@/util/push/unipush.js";
	export default {
		//全局监听websocket对象在线状态
		watch:{
			vuex_WSObj(newVal){
				let that=this;
				if(newVal==null||newVal==undefined){
					//console.log("===================监听到websocket掉线了==========");
				}else{
					console.log("===========监听到websocket已经重新建立链接==========");
					this.vuex_WSObj.onMessage(function(res) {
						that.handleWebsocketMsg(res);
					});
				}
			}
		},
		onLaunch: function() {
			let that=this;
			plus.push.setAutoNotification(true);
			//用于推送的，暂时没有对接
			this.$u.vuex('vuex_deviceMode','mobile_'+new Date().getTime());
			this.initWsListener();//初始化websocket监听器
			// #ifdef APP-PLUS
			//this.getAppConfig();//全局获取APP应用配置
			// #endif
			// #ifdef H5
			this.getQueryString();
			// #endif
			let plat=  uni.getSystemInfoSync().platform;
			console.log("=============当前应用平台==============",plat);
			this.$u.vuex("vuex_OSPlat",plat);
			uniPushListener.getCid(that);
			uniPushListener.pushListener(that); // 监听通知栏信息
		},
		onHide: function() {
			let that=this;
			let plat=  uni.getSystemInfoSync().platform;
			reStartTimestamp=0;
			if(reStartTimer){
				clearInterval(reStartTimer);
				reStartTimer=null;
			}
			reStartTimer= setInterval(function(){
				//console.log("reStartTimestamp",reStartTimestamp);
				reStartTimestamp++;
				if(reStartTimestamp>=15){
					clearInterval(reStartTimer);
					that.globalWX.closeWS();
					uni.switchTab({
						url:'/pages/tabbar/group/group'
					})
					return;
				}
			},1000)
		},
		onUnload:function(){
			console.log('App onUnload')
			this.globalWX.closeWS();
		},
		onShow:function(){
			let that=this;
			if(that.vuex_token&&that.vuex_token.length>0&&that.vuex_userInfo&&that.vuex_userInfo.id){
				that.globalWX.closeWS();
				let param={
					userId:that.vuex_userInfo.id,
					chatGroupId:"00000000",
					token:that.vuex_token,
				}
				that.globalWX.initWebScoket(param);
				uni.$emit("updateGroupList",null);
			}
			reStartTimestamp=0;
			if(reStartTimer){
				clearInterval(reStartTimer);
				reStartTimer=null;
			}
		},
		methods:{
			//监听WS信息
			initWsListener:function(){
				let that=this;
				if(that.vuex_WSObj!=null&&that.vuex_WSObj!=undefined){
					that.vuex_WSObj.onMessage(function(res) {
						that.handleWebsocketMsg(res);
					});
				}else{
					console.log("长连接尚未建立或者已经掉线");
				}		
			},
			//对webScoket信息进行处理
			handleWebsocketMsg:function(msg){
				let  that=this;
				//console.log("监听到后端webscoket信息",msg);
				let contentStr= msg.data;
				let contentObj= JSON.parse(contentStr);
				let code=contentObj.code;
				//处理不同的信息类型
				switch (code){
					case that.messageApi.BUSINESS_CODE.ping:
						//console.log("接收到心跳");
						return;
					case that.messageApi.BUSINESS_CODE.launchGroup:
						//发起群聊
						that.messageApi.handleChatMsg(contentObj.message,that);
						break;
					case that.messageApi.BUSINESS_CODE.deleteGroup:
						//解散群聊
						that.messageApi.handleDeleteGroup(contentObj.message,that);
						break;	
					case that.messageApi.BUSINESS_CODE.chatting:
						//console.log("===监听到用户聊天信息===",contentObj);
						that.messageApi.handleChatMsg(contentObj.message,that);
						break;
					case that.messageApi.BUSINESS_CODE.AI:
						that.messageApi.handleAIMessage(contentObj.message,that);
						break;
					case that.messageApi.BUSINESS_CODE.FriendBindChange:
						that.messageApi.handleContactMessage(contentObj.message,that);
						break;	
					case that.messageApi.BUSINESS_CODE.DeleteFriend:
						that.messageApi.handleDelFriendMessage(contentObj.message,that);
						break;
					case that.messageApi.BUSINESS_CODE.FriendCircle:
						that.messageApi.handleFriendCircleMessage(contentObj.message,that);
						break;
					case that.messageApi.BUSINESS_CODE.Calling:
						that.messageApi.handleCallingMessage(contentObj.message,that);
						break;	
					default:
						break;
				}
				console.log("==========!!!=============监听到长连接的信息,信息业务类型【"+code+"】=========!!!======")	
			},

			//获取APP配置信息，仅限于APP
			getAppConfig:function(){
				let configData={};
				this.$u.api.sys.getAppConfig({}).then(res => {
					console.log("获取APP配置信息结果",res);
					if(res.code==200){
						let configArr= res.data;
						if(configArr&&configArr.length>0){
							for (let i = 0; i < configArr.length; i++) {
								let config= configArr[i];
								let key= config.configKey;
								let value= config.configValue
								configData[key]=value;
							}
						}
						this.$u.vuex("vuex_app_config",configData);
						console.log("app配置信息",this.vuex_app_config);
					}
				})
			},
			//获取路径上的参数，仅限于H5
			getQueryString: function() {
				let args = {}; // 保存参数数据的对象
				let href = window.location.href;
				if (href.indexOf('?') !== -1) {
					let qs = href.substring(href.indexOf('?') + 1, href.length);
					let items = qs.length > 0 ? qs.split("&") : []; // 取得每一个参数项,
					let item = null;
					let len = items.length;
					for (var i = 0; i < len; i++) {
						item = items[i].split("=");
						let name = decodeURIComponent(item[0]);
						let value = decodeURIComponent(item[1]);
						if (name) {
							args[name] = value;
						}
					}
				}
				console.log("路径上的参数", args);
				return args;
			},
		}
	}
</script>

<style>
	@import "./static/iconfont/iconfont.css";
	@import "./static/music/iconfont.css";
</style>
<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "uview-ui/index.scss";
	.my-cell-hover-class {
		background-color: #cccccc !important;
	}
	.none-hover{
		background-color: #ffffff !important;
	}
	.test-border{
		border:1rpx solid red;
	}
	.my-hover-class {
		opacity: 0.8;
	}
	
	.hidden-scroll-bar{
			::-webkit-scrollbar{
				display: none;
				height: 0;
				width: 0;
			}
	}
</style>
